package com.citrix.gs.util.beacon.job;

import java.text.MessageFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.citrix.gs.util.RepositoryDeployer;
import com.citrix.gs.util.beacon.BeaconJobType;
import com.citrix.gs.util.beacon.JobConfig;
import com.citrix.gs.util.common.Config;

public class BeaconConfigJob extends BeaconJob implements Deployable {
    
    @SuppressWarnings("unused")
    private static Logger logger = LoggerFactory.getLogger(BeaconConfigJob.class);

    public BeaconConfigJob(RepositoryDeployer deployer) {
        super(deployer);
        this.jobType = BeaconJobType.Config;
        this.jobName = this.jobType.name() + "-" + repo.getName();
        this.tempFile = jobType.name().toLowerCase() + "_template.xml";
    }

    @Override
    public void add() {
        
        
        String clientName = JobConfig.getTestClientPrefix() + "-" + jobName ;
        String projectPath = MessageFormat.format(Config.me().getProperty("test.toolconfig.veiwmap"), repo.getPath(true), clientName);
        
        if(beforeAdd() && editPerforceSCM( 
                Config.me().getProperty("platform.perforce.username"), 
                Config.me().getProperty("platform.perforce.ticket"), 
                Config.me().getProperty("platform.perforce.server"), clientName, projectPath) // Edit SCM(perforce part)
                && editAssignedNode() // Edit assignedNode
                && editUpstreamJob(BeaconJobType.Src.name())// watch upstream job
                //&& editNextJob(BeaconJobType.Toolset.name()) // Edit next job
                && editNodeStalkerBuildWrapper() // use same workspace with src job
                && createJob() // create job
                ){
            
        } else {
            
        }
        
    }

    @Override
    public void move() {
        if(beforeMove() && updateClient() && updateProjectPath() && moveJob()) {
            
        } else {
            
        }
    }
    
    public boolean updateProjectPath(){
        
        return updateProjectPath("BeaconToolConfig_TEST", "BeaconToolConfig");
    }

}
